/*
 * Copyright (C) 2023 HiHope Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 #include <stdio.h>
 #include <unistd.h>
 
 #include "cmsis_os2.h"
 #include "ohos_init.h"
 #include "iot_gpio.h"
 
 #define LED_TASK_GPIO 6
 #define LED_TASK_STACK_SIZE 1024
 #define LED_TASK_PRIO 25
 #define GPIO_HIGH 1
 #define GPIO_LOW 0
 
 /* SOS信号时间定义 */
 #define DOT_TIME 100    // 短点时间（毫秒）
 #define DASH_TIME 300   // 长点时间（毫秒）
 #define SYMBOL_GAP 100  // 符号间隔（毫秒）
 #define LETTER_GAP 300  // 字母间隔（毫秒）
 #define WORD_GAP 500    // 单词间隔（毫秒）
 
 /* 发送一个点（短） */
 static void SendDot(void)
 {
     IoTGpioSetOutputVal(LED_TASK_GPIO, GPIO_HIGH);
     osDelay(DOT_TIME);
     IoTGpioSetOutputVal(LED_TASK_GPIO, GPIO_LOW);
     osDelay(SYMBOL_GAP);
 }
 
 /* 发送一个划（长） */
 static void SendDash(void)
 {
     IoTGpioSetOutputVal(LED_TASK_GPIO, GPIO_HIGH);
     osDelay(DASH_TIME);
     IoTGpioSetOutputVal(LED_TASK_GPIO, GPIO_LOW);
     osDelay(SYMBOL_GAP);
 }
 
 /* 发送字母S */
 static void SendS(void)
 {
     SendDot();
     SendDot();
     SendDot();
     osDelay(LETTER_GAP);
 }
 
 /* 发送字母O */
 static void SendO(void)
 {
     SendDash();
     SendDash();
     SendDash();
     osDelay(LETTER_GAP);
 }
 
 /* 发送SOS信号 */
 static void SendSOS(void)
 {
     SendS();
     SendO();
     SendS();
     osDelay(WORD_GAP);
 }
 
 static void* GpioTask(void)
 {
     IoTGpioInit(LED_TASK_GPIO);
     IoTGpioSetDir(LED_TASK_GPIO, IOT_GPIO_DIR_OUT);
 
     while (1) {
         printf("Sending SOS signal...\n");
         SendSOS();
     }
     return NULL;
 }
 
 static void GpioExampleEntry(void)
 {
     osThreadAttr_t attr;
 
     attr.name = "GpioTask";
     attr.attr_bits = 0U;
     attr.cb_mem = NULL;
     attr.cb_size = 0U;
     attr.stack_mem = NULL;
     attr.stack_size = LED_TASK_STACK_SIZE;
     attr.priority = LED_TASK_PRIO;
 
     if (osThreadNew((osThreadFunc_t)GpioTask, NULL, &attr) == NULL) {
         printf("[GpioExample] Failed to create GpioTask!\n");
     }
 }
 
 SYS_RUN(GpioExampleEntry);
 